<html>
<head>
<meta http-equiv=Content-Type content=text/html;charset=utf-8>
<#include "../inc/commonJs.html">
<script language="JavaScript">
	function doSave(){
		endEditing();
		var a = $('#tt').datagrid('getData').rows;
		$('#contact').val(JSON.stringify(a));
		var url = "<#if id??>editCustom.do?id=${id}<#else>addCustom.do</#if>";
		$('form').form('submit',{  
		    url:url,
		    onSubmit:function(){
				return $(this).form('validate')
		    },  
		    success:function(data){  
				top.closemodalwindow(true,data);
		    }  
		});
	}
	//
	$(function(){
		$('#type').combobox('setValue','${obj.type!}');
		$('#owner').combobox('setValue','${obj.owner!}');
		$('#tt').datagrid({  
		    iconCls:'icon-edit',  
		    height:150,  
		    singleSelect:true,  
		    idField:'itemid',  
		    url:'listContacts.do',  
		    queryParams :{customId:'${obj.id!}'},
		    columns:[[  
		    	{field:'id',title:'ID',width:120,align:'center', hidden:true},
		        {field:'name',title:'姓名',width:120,align:'center', editor:'text'},  
		        {field:'duty',title:'职务',width:120,align:'center',editor:'text'},  
		        {field:'tel',title:'手机',width:150,align:'center',editor:'text'},  
		        {field:'phone',title:'电话',width:150,align:'center',editor:'text'},  
		        {field:'email',title:'电子邮箱 ',width:150,align:'center',editor:'text'},
		        {field:'status',title:'操作',width:70,align:'center',
		        	formatter: function(value,rowdata,index){
		        		return '<a id="btnadd" class="l-btn l-btn-plain" href="javascript:void(0)"><span class="l-btn-left"><span class="l-btn-text icon-no l-btn-icon-left" onclick="removeit('+index+','+rowdata.id+')"></span></span></a>'
		        	}
		        }
		    ]],  
		    onBeforeEdit:function(index,row){  
		        row.editing = true;  
		        $('#tt').datagrid('refreshRow', index);  
		    },  
		    onAfterEdit:function(index,row){  
		       row.editing = false;  
		       $('#tt').datagrid('refreshRow', index);  
		    },  
		    onCancelEdit:function(index,row){  
		        row.editing = false;  
		        $('#tt').datagrid('refreshRow', index);  
		    },
		    onClickRow:function(rowIndex, rowData){
		    	onClickRow(rowIndex);
		    }
		});
	})
	var editIndex = undefined;
	function onClickRow(index){
			if (editIndex != index){
				if (endEditing()){
					$('#tt').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
					editIndex = index;
				} else {
					$('#tt').datagrid('selectRow', editIndex);
				}
			}
		}
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#tt').datagrid('validateRow', editIndex)){
			var nameEd = $('#tt').datagrid('getEditor', {index:editIndex,field:'name'});
			var name = $(nameEd.target).val();
			$('#tt').datagrid('getRows')[editIndex]['name'] = name;
			var dutyEd = $('#tt').datagrid('getEditor', {index:editIndex,field:'duty'});
			var duty = $(dutyEd.target).val();
			$('#tt').datagrid('getRows')[editIndex]['duty'] = duty;
			var telEd = $('#tt').datagrid('getEditor', {index:editIndex,field:'tel'});
			var tel = $(telEd.target).val();
			$('#tt').datagrid('getRows')[editIndex]['tel'] = tel;
			var phoneEd = $('#tt').datagrid('getEditor', {index:editIndex,field:'phone'});
			var phone = $(phoneEd.target).val();
			$('#tt').datagrid('getRows')[editIndex]['phone'] = phone;
			var emailEd = $('#tt').datagrid('getEditor', {index:editIndex,field:'email'});
			var email = $(emailEd.target).val();
			$('#tt').datagrid('getRows')[editIndex]['email'] = email;
			
			$('#tt').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	}
	function addContact(){
		if(endEditing()){
			editIndex = $('#tt').datagrid('getRows').length;
			$('#tt').datagrid('appendRow',{})
			$('#tt').datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);
		}
	}
	function removeit(i,id){
		if (editIndex == undefined){return}
		if(id){
			$.post("${request.contextPath}/xadmin/custom/delContacts.do", { "id": id},
			   function(data){
			   	
			});
		}
		$('#tt').datagrid('cancelEdit', editIndex).datagrid('deleteRow', i);
		editIndex = undefined;
	}
</script>

</head>
<body>
<div class="easyui-panel" data-options='fit:true,border:false'> 
	<form id='form1' name="form1" method="post" action="newsSave.do" OnSubmit="return checkdata()">
		<input type="hidden" name="contact" id="contact">
		<table border="0" cellspacing="0" width="99%" cellpadding="0" class="table_all_border"  style=" margin:5px 5px 5px 5px ; border-collapse: collapse; border:#ccc 1px solid;">
		<tr> 
			<td colspan="8"   class="td_table_top" align="center" >
				客户信息
			</td>
		</tr>
		<tr height="35"> 
			<td align="right" class="td_table_1" valign="middle"><font color="#FF0000">*</font>客户名：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="name" type="TEXT" value="${obj.name!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
			<td align="right" class="td_table_1" valign="middle"><font color="#FF0000">*</font>客户类型：</td>
			<td class="td_table_2" colspan="3"> 
				<input id="type" name="type" class="easyui-combobox"  style="width:230px;" panelHeight:"auto" data-options="valueField:'mxid',textField:'mxmc',url:'${request.contextPath}/xadmin/custom/listCustomTypeForCombox.do'" />
			</td>
		</tr>
		
		<tr height="35"> 
			<td align="right" class="td_table_1" valign="middle">客户编号：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="code" type="TEXT" value="${obj.code!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
			<td align="right" class="td_table_1" valign="middle">所有者：</td>
			<td class="td_table_2" colspan="3"> 
				<input id="owner" name="owner" class="easyui-combobox"  style="width:230px;" panelHeight:"auto" data-options="valueField:'userid',textField:'username',url:'${request.contextPath}/xadmin/system/listUserForCombox.do'" />
			</td>
		</tr>
		<tr height="35"> 
			<td align="right" class="td_table_1" valign="middle">公司电话：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="tel" type="TEXT" value="${obj.tel!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
			<td align="right" class="td_table_1" valign="middle">公司传真：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="fax" type="TEXT" value="${obj.fax!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
		</tr>
		<tr height="35"> 
			<td align="right" class="td_table_1" valign="middle">邮编：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="zipcode" type="TEXT" value="${obj.zipcode!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
			<td align="right" class="td_table_1" valign="middle">公司地址：</td>
			<td class="td_table_2" colspan="3"> 
				<input name="adder" type="TEXT" value="${obj.adder!}" size=30 maxlength=100 style=" height:22;background-color:ffffff;border: 1 double; border-color:#BDD0FD;" >
			</td>
		</tr>
		
		<tr> 
			<td colspan="8"   class="td_table_top" align="center" >
				联系人
			</td>
		</tr>
		<tr height="35"> 
			<td align="center" colspan="8" valign="middle">
			<table id="tt"></table>
			</td>
		</tr>
		<tr height="35"> 
			<td align="left" colspan="6" valign="middle">
				<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addContact();">增加联系人</a>
			</td>
		</tr>
		<tr> 
			<td colspan="8"   class="td_table_top" align="center" >
				备注
			</td>
		</tr>
		<tr height="35">
			<td align="right" class="td_table_1" valign="middle">备注：</td> 
			<td colspan="5" align="left" class="td_table_2">
				<textarea rows="3" cols="100" name="content">${obj.content!}</textarea>
			</td>
		</tr>
	</table>
	</form>
</div>
</body>
</html>